#include"../include/hash.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>



/*int main()
{
	char fileName[20];
	int i=0,ch;
	int flag=1;
	//printf("\n start \n");
	//for(i=0;i<26;i++)
	//strcpy(hashtable[i]->fName,"");

	

	while(flag)
	{

	printf("1.Insert\n2.Display\n3.Exit\nEnter choice:");
	scanf("%d",&ch);
	switch(ch)
	{
	case 1: printf("Enter file name:");
		scanf("%s",fileName);
		int hashIndex=hash(fileName);
		printf("Index returned:%d\n",hashIndex);
		struct fileDescriptor *fd=(struct fileDescriptor *)malloc(sizeof(struct fileDescriptor));
		strcpy(fd->fileName,fileName);
		insert(fd,hashIndex);	
		break;

	case 2:printHash();
		break;

	case 3:flag=0;
	}
	}

return 0;
}
*/

int hash(char s[])
{
	char c=s[0];
if(c>64 && c<91)
return c-65;

if(c>96 && c<123) 
return c-97;

if(c!=47)
return 26;
       
} 

void insert(struct fileDescriptor *fdhash, int index)
{


char name[20];
strcpy(name,fdhash->fileName);
//printf("in hash while inserting file name :%s\n",fdhash->fileName);

int found=0;
struct linkedlist *head=NULL,*temp,*temp1,*temp2,*temp3;



temp1=malloc(sizeof(struct linkedlist));
strcpy(temp1->fName,name);
temp1->fd=fdhash;
//printf("in hash while inserting Locationpath :%s\n",temp1->fd->locationPath);


temp2=temp3=hashtable[index];

if(temp2==NULL)
{

temp2=temp1;
temp2->nextNode=temp3;
hashtable[index]=temp2;

}

else
{

while(temp3->nextNode!=NULL)
{
temp3=temp3->nextNode;
}

temp1->nextNode=NULL;
temp3->nextNode=temp1;
}

//printf("File name %s\n",name);
}
void flushHash()
{

int i;
struct linkedlist *p,*n;

		for(i=0;i<27;i++)
		{
		p=hashtable[i];

				while(p!=NULL)
				{
				n=p;
				p=p->nextNode;
				free(n);
				}
		hashtable[i]=NULL;
		}

}

void printHash()
{
//printf("in printhash\n");
int i;
struct linkedlist *p;
for(i=0;i<27;i++)
{
p=hashtable[i];
//if(p!=NULL)
//printf("Index: %d",i);
if(p==NULL)
{
printf("-> NULL");
}
while(p!=NULL)
{
//printf("-> %s ",p->fName);
printf("-> %s ",p->fName);
p=p->nextNode;
}
printf("\n");
}


}


void deleteHash(struct fileDescriptor *fdhash)
{
char name[20];
int index,found=1;
int val1,val2;
struct linkedlist *temp4,*prev,*next;
struct fileDescriptor fdd,*tempfd;

//printf("In the deleteHASH\n");
strcpy(name,fdhash->fileName);

index=hash(name);
temp4=hashtable[index];
tempfd=temp4->fd;
fdd=*tempfd;


//printf("%ld\n",temp4);
//printf("%ld\n",tempfd);

//printf("Index: %d\n",index);

if(temp4==NULL)
{

//printf("File not present\n");

}

else
{


val1=strcmp(tempfd->locationPath,fdhash->locationPath);
val2=strcmp(tempfd->fileName,fdhash->fileName);
//printf("hash: %s fd passes: %s\n",tempfd->locationPath,fdhash->locationPath);
//printf("%d %d\n",val1,val2);

if(val1==0 && val2==0)
{
found=0;
hashtable[index]=temp4->nextNode;
free(temp4);
//printf("in 1st if loop\n");
}

else
{
while(found==1 && temp4->nextNode!=NULL)
{

val1=strcmp(tempfd->locationPath,fdhash->locationPath);
val2=strcmp(tempfd->fileName,fdhash->fileName);
//printf("before if loop\n");
if(val1==0 && val2==0)
{
found=0;
}
else
{
prev=temp4;
temp4=temp4->nextNode;
tempfd=temp4->fd;
//printf("%ld\n",temp4);
next=temp4->nextNode;
}


}

//printf("out of while loop\n");
prev->nextNode=next;
free(temp4);

}
}


}
